package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.FishPondTaskData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/10/18 1:46
 */
public class UserFishPondTaskDataDaoImpl extends TempDataObjectDao<FishPondTaskData> {

    private static String insertSql = "insert into t_u_fishpond_task(userId,id,taskId,npcId,textIndex,eventId,value,isGetReward,extendParam) " +
            "values(?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(FishPondTaskData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTaskId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTextIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue().longValue()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));

        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    private static String updateSql = "update t_u_fishpond_task set taskId=?,npcId=?,textIndex=?,eventId=?,value=?,isGetReward=?,extendParam=? where userId=? and id=?";
    @Override
    protected boolean updateData(FishPondTaskData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getTaskId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTextIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue().longValue()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected FishPondTaskData parseData(ResultSet rs) throws SQLException {
        FishPondTaskData data = new FishPondTaskData();
        data.setUserId(rs.getLong("userId"));
        data.setId(rs.getLong("id"));
        data.setTaskId(rs.getInt("taskId"));
        data.setNpcId(rs.getInt("npcId"));
        data.setTextIndex(rs.getInt("textIndex"));
        data.setEventId(rs.getInt("eventId"));
        data.setValue(BigInteger.valueOf(rs.getLong("value")));
        data.setGetReward(rs.getBoolean("isGetReward"));
        data.setExtendParam(rs.getString("extendParam"));
        return data;
    }

    private static String getSql = "select * from t_u_fishpond_task where userId = ?";

    public List<FishPondTaskData> getFishPondTaskDataList(long playerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<FishPondTaskData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, playerId);
            rs = call.executeQuery();
            while (rs.next()) {
                FishPondTaskData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
